import { Component, OnInit, Input } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { GroupService } from 'src/app/core/service/group.service';
import { UserService } from 'src/app/core/service/user.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-org-edit-user',
  templateUrl: './org-edit-user.component.html',
  styleUrls: ['./org-edit-user.component.scss']
})
export class OrgEditUserComponent implements OnInit {
  @Input() email: string;
  @Input() firstName: string;
  @Input() lastName: string;
  @Input() groupUuid: string;
  @Input() roleUuid: string;
  @Input() userUuid: string;

  teamList: any;
  groupList: any;
  roleGroup: any;
  userForm: FormGroup;
  info = {};

  constructor(
    private modal: NzModalRef,
    private groupService: GroupService,
    private userService: UserService,
    private nzMessageService: NzMessageService,
    fb: FormBuilder
  ) {
    this.userForm = fb.group({
      email: [null, Validators.compose([Validators.required, Validators.email])],
      lastName: [null, Validators.compose([Validators.required])],
      firstName: [null, Validators.compose([Validators.required])],
      groupUuid: [null, Validators.compose([Validators.required])],
      roleUuid: [null, Validators.compose([Validators.required])],
    });
  }

  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }

  ngOnInit() {
    this.getGroup();
    this.getRole();
    this.userForm.patchValue({
      email: this.email,
      firstName: this.firstName,
      lastName: this.lastName,
      groupUuid: this.groupUuid,
      roleUuid: this.roleUuid
    });
  }

  /**
   * 获取所属小组
   */
  getGroup() {
    this.groupService.getGroup()
      .subscribe((groupInfo) => {
        this.groupList = groupInfo;        // 数据集
        if (this.groupList) {
          if (!this.userForm.controls.groupUuid.value) {
            this.userForm.controls.groupUuid.setValue(this.groupList[0].groupUuid);
          }
        }
      });
  }

  /**
   * 获取角色
   */
  getRole() {
    this.groupService.getRole()
      .subscribe((roleGroup) => {
        this.roleGroup = roleGroup;        // 数据集
        if (this.roleGroup) {
          if (!this.userForm.controls.roleUuid.value) {
            this.userForm.controls.roleUuid.setValue(this.roleGroup[0].roleUuid);
            console.log('=== getRole().this.roleGroup[0].roleUuid  ===>', this.roleGroup[0].roleUuid);
          }
        }
      });
  }

  /**
   * 编辑
   */
  updateUser() {
    console.log('提交', this.userForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.userForm.controls) {
      this.userForm.controls[c].markAsTouched();
    }
    if (this.userForm.valid) {
      this.info = {
        email: this.userForm.controls.email.value,
        lastName: this.userForm.controls.lastName.value,
        firstName: this.userForm.controls.firstName.value,
        groupUuid: this.userForm.controls.groupUuid.value,
        roleUuid: this.userForm.controls.roleUuid.value,
        userUuid: this.userUuid
      };
      this.editService(this.info);
    }
  }

  private editService(info: any) {
    this.userService.updateUser(this.info)
      .subscribe((userData) => {
        this.resultService(userData, '修改');
      });
  }

  private resultService(data: any, msg: string) {
    if (data.status) {
      this.nzMessageService.success(msg + '成功!', {
        nzDuration: 10000
      });
      this.modal.destroy();
    } else {
      this.nzMessageService.error(msg + '失败!请检查后重新填写', {
        nzDuration: 10000
      });
    }
  }


}


